/*
 * @Author: DaQing Lu
 * @Date: 2020-01-24 14:15:34
 * @LastEditors: DaQing Lu
 * @LastEditTime : 2020-02-15 13:22:55
 * @Description: file content
 */
import React, {useState, useEffect} from 'react';
import action from '../../store/actions/unpaidOrderLibrary';
import {connect} from 'react-redux';
import {Pagination} from 'antd';
import {getOrderUnpaiDetais} from '../../api';
import Header from './Header';
import Search from './Search';
import UnpaidOrderList from './UnpaidOrderList';
import Details from './UnpaidOrderDetails';
import Edit from './UnpaidOrderEdit';
import Audit from './UnpaidOrderAudit';

function UnpaidOrderLibrary(props) {
  const [detailsVisible, setDetailsVisible] = useState(false);
  const [editVisible, setEditVisible] = useState(false);
  const [auditVisible, setAuditVisible] = useState(false);
  useEffect(() => {
    props.GET_ORDER_LIBRARY();
  }, []);

  // 详情
  const details = order_no => {
    props.GET_UNPAR_SINGLE_DETAILS(order_no).then(res => {
      setDetailsVisible(true);
    }).catch(err => {
      console.log(err);
    });

  };
  // 编辑
  const [order_no, setOrderNo] = useState(null);
  const [goods_price, setGoodPrice] = useState(null);
  const [freight_price, setFrieightPrice] = useState(null);
  const [remark, setRemark] = useState(null);

  const edit = async (order_no, goods_price, freight_price, remark) => {
    setOrderNo(order_no);
    setGoodPrice(goods_price);
    setFrieightPrice(freight_price);
    setRemark(remark);
    setEditVisible(true);
  };

  const priceChange = value => {
    setGoodPrice(value);
  };
  const freightChange = value => {
    setFrieightPrice(value);
  };
  const remarkChange = value => {
    setRemark(value);
  };
  // 通过审核按钮
  const audit = async (refund_no) => {
    setAuditVisible(true);
    return;
    // checkRefund(refund_no).then(res => {
    //   props.GET_ORDER_LIBRARY(props.current);
    // }).catch(err => {
    //   console.log(err);
    // });
  };

  // ok
  const handleOk = async key => {
    switch (key) {
      case 1:
        setDetailsVisible(false);
        break;
      case 2:
        try {
          // 修该接口处理
          await getOrderUnpaiDetais(order_no, goods_price, freight_price, remark);
          // 修改成功后的处理
          await props.GET_ORDER_LIBRARY(props.current, undefined, searchValue);
          setEditVisible(false);
        } catch (error) {
          console.log(error);
        }
      case 3:
        setAuditVisible(false);
        break;
      default:
        break;
    }

  };
  // cancel
  const handleCancel = key => {
    switch (key) {
      case 1:
        setDetailsVisible(false);
        break;
      case 2:
        setEditVisible(false);
        break;
      case 3:
        setAuditVisible(false);
        break;
      default:
        break;
    }
  };

  // 分页改变
  const pageChange = page => {
    props.GET_ORDER_LIBRARY(page, undefined, searchValue);
  };

  /**
   * 搜索内容
   * @param searchValue
   */
  const [searchValue, setSearchValue] = useState('');
  const search = async value => {
    if (value) {
      await setSearchValue(value);
      console.log(props.current);
      props.GET_ORDER_LIBRARY(1, undefined, value);
    }
  };

  const changeSearchValue = value => {
    setSearchValue(value);
  };

  const clearSearch = () => {
    setSearchValue('');
    props.GET_ORDER_LIBRARY();
  };


  return (
    <div className='returnReview'>
      <Header/>
      <Search
        search={search}
        searchValue={searchValue}
        changeSearchValue={changeSearchValue}
        clearSearch={clearSearch}
      />
      <UnpaidOrderList
        unpaidOrderList={props.unpaidOrderList}
        details={details}
        edit={edit}
        audit={audit}
      />
      <Pagination
        current={props.current}
        onChange={pageChange}
        total={props.pageCount ? props.pageCount : 1}
        style={{textAlign: 'center', marginTop: '20px'}}
        pageSize={10}
        hideOnSinglePage
      />
      <Details data={props.unpaidOrderDetails} visible={detailsVisible} handleOk={handleOk}
               handleCancel={handleCancel}/>
      <Edit
        visible={editVisible}
        handleOk={handleOk}
        handleCancel={handleCancel}
        order_no={order_no}
        goods_price={goods_price}
        freight_price={freight_price}
        remark={remark}
        priceChange={priceChange}
        freightChange={freightChange}
        remarkChange={remarkChange}
      />
      <Audit visible={auditVisible} handleOk={handleOk} handleCancel={handleCancel}/>

    </div>
  );
}

const mapStateToProps = state => state.unpaidOrderLibrary;
export default connect(mapStateToProps, action)(UnpaidOrderLibrary);
